package com.nogroup.springbootproject.service.impl;

import com.nogroup.springbootproject.entity.Campus;
import com.nogroup.springbootproject.dao.CampusDao;
import com.nogroup.springbootproject.service.CampusService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * (Campus)表服务实现类
 *
 * @author makejava
 * @since 2024-04-07 18:45:32
 */
@Service("campusService")
@Transactional(rollbackFor = Exception.class)
public class CampusServiceImpl implements CampusService {
    @Resource
    private CampusDao campusDao;

    /**
     * 通过ID查询单条数据
     *
     * @param campusCode 主键
     * @return 实例对象
     */
    @Override
    public Campus queryById(String campusCode) {
        return this.campusDao.queryById(campusCode);
    }

    /**
     * 分页查询
     *
     * @param campus 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Campus> queryByPage(Campus campus, PageRequest pageRequest) {
        long total = this.campusDao.count(campus);
        return new PageImpl<>(this.campusDao.queryAllByLimit(campus, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param campus 实例对象
     * @return 实例对象
     */
    @Override
    public Campus insert(Campus campus) {
        this.campusDao.insert(campus);
        return campus;
    }

    /**
     * 修改数据
     *
     * @param campus 实例对象
     * @return 实例对象
     */
    @Override
    public Campus update(Campus campus) {
        this.campusDao.update(campus);
        return this.queryById(campus.getCampusCode());
    }

    /**
     * 通过主键删除数据
     *
     * @param campusCode 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String campusCode) {
        return this.campusDao.deleteById(campusCode) > 0;
    }
}
